package com.gordon.wiki.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gordon.wiki.pojo.Category;
import com.gordon.wiki.pojo.Ebook;
import com.gordon.wiki.req.CategoryQueryReq;
import com.gordon.wiki.req.EbookQueryReq;
import com.gordon.wiki.resp.CategoryQueryResp;
import com.gordon.wiki.resp.EbookResp;
import com.gordon.wiki.resp.PageResp;
import com.gordon.wiki.result.Result;
import com.gordon.wiki.result.ResultUtil;
import com.gordon.wiki.service.CategoryService;
import com.gordon.wiki.service.EbookService;
import com.gordon.wiki.utils.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Projectname: wiki
 * @Filename: CategoryController
 * @Author: GordonLo
 * @Data:2022/11/11 14:11
 * @Description: TODO
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private EbookService ebookService;

    @GetMapping("/getCategoryListByPage")
    public Result getCategoryListByPage(CategoryQueryReq req) {
        Page<Category> page = new Page<>(req.getPage(), req.getSize());
        page = categoryService.page(page);
        List<Category> list = page.getRecords();

        LOG.info("总行数：{}", page.getTotal() + "");
        LOG.info("总页数：{}", page.getPages() + "");

        List<CategoryQueryResp> ebookResps = CopyUtil.copyList(list, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> respPage = new PageResp<>();
        respPage.setTotal(page.getTotal());
        respPage.setList(ebookResps);

        return ResultUtil.success(respPage);
    }

    @PostMapping("/save")
    public Result save(@RequestBody CategoryQueryReq req) {

        Category category = CopyUtil.copy(req, Category.class);
        categoryService.saveOrUpdate(category);
        return ResultUtil.success();
    }

    @DeleteMapping("/remove")
    public Result remove(int id) {

        categoryService.removeById(id);


        return ResultUtil.success();
    }

    @GetMapping("/allList")
    public Result allList(CategoryQueryReq req) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        List<Category> list = categoryService.list(wrapper);

        List<CategoryQueryResp> resp = CopyUtil.copyList(list, CategoryQueryResp.class);
//
//        CommonResp<List<CategoryQueryResp>> listCommonResp = new CommonResp<>();
//        listCommonResp.setContent(resp);
        return ResultUtil.success(resp);
    }


    @GetMapping("/getEbookListByPage")
    public Result getEbookListByPage(EbookQueryReq req) {
        Page<Ebook> page = new Page<>(req.getPage(), req.getSize());

        if (!ObjectUtils.isEmpty(req.getCategory2_id())) {
            QueryWrapper<Ebook> wrapper = new QueryWrapper<>();
            wrapper.like("category2_id", req.getCategory2_id());
            page = ebookService.page(page, wrapper);
        } else {
            page = ebookService.page(page);
        }

        List<Ebook> list = page.getRecords();

        LOG.info("总行数：{}", page.getTotal() + "");
        LOG.info("总页数：{}", page.getPages() + "");

//        List<EbookQueryResp> ebookResps = CopyUtil.copyList(list, EbookQueryResp.class);
//
//        PageResp<EbookQueryResp> respPage = new PageResp<>();
//        respPage.setTotal(page.getTotal());
//        respPage.setList(ebookResps);
//
//        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
//        resp.setContent(respPage);

//        return ResultUtil.success();

        List<EbookResp> ebookResps = CopyUtil.copyList(list, EbookResp.class);

        PageResp<EbookResp> respPageResp = new PageResp<>();
        respPageResp.setTotal(page.getTotal());
        respPageResp.setList(ebookResps);

//        CommonResp<Page<Ebook>> resp = new CommonResp<>();
//        resp.setContent(page);
        return ResultUtil.success(respPageResp);
    }


}